package com.hik.CmsService;

import com.hik.AlarmService.AlarmDemo;
import com.hik.Common.PropertiesUtil;
import com.hik.Common.osSelect;
import com.hik.Boot.IsupBoot;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import java.io.*;

/**
 * @author jiangxin
 * @create 2022-04-29-13:57
 */
public class CmsDemo {

    public static HCISUPCMS hCEhomeCMS = null;
    public static int CmsHandle = -1; //CMS监听句柄
    static FRegisterCallBack fRegisterCallBack;//注册回调函数实现
    HCISUPCMS.NET_EHOME_XML_REMOTE_CTRL_PARAM struRemoteCtrl = new HCISUPCMS.NET_EHOME_XML_REMOTE_CTRL_PARAM();
    HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM struCMSListenPara = new HCISUPCMS.NET_EHOME_CMS_LISTEN_PARAM();
    HCISUPCMS.BYTE_ARRAY m_struInbuffer;
    HCISUPCMS.BYTE_ARRAY m_struOutbuffer;
    HCISUPCMS.BYTE_ARRAY m_struStatusBuffer;
    static String configPath = "./config.properties";
    PropertiesUtil propertiesUtil;

    {
        try {
            propertiesUtil = new PropertiesUtil(configPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * cms服务初始化，开启监听
     *
     * @throws IOException
     */
    public void cMS_Init() throws IOException {

        if (hCEhomeCMS == null) {
            if (!CreateSDKInstance()) {
                System.out.println("Load CMS SDK fail");
                return;
            }
        }
        if (osSelect.isWindows()) {
            HCISUPCMS.BYTE_ARRAY ptrByteArrayCrypto = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathCrypto = System.getProperty("user.dir") + "\\lib\\libeay32.dll"; //Linux版本是libcrypto.so库文件的路径
            System.arraycopy(strPathCrypto.getBytes(), 0, ptrByteArrayCrypto.byValue, 0, strPathCrypto.length());
            ptrByteArrayCrypto.write();
            hCEhomeCMS.NET_ECMS_SetSDKInitCfg(0, ptrByteArrayCrypto.getPointer());

            //设置libssl.so所在路径
            HCISUPCMS.BYTE_ARRAY ptrByteArraySsl = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathSsl = System.getProperty("user.dir") + "\\lib\\ssleay32.dll";    //Linux版本是libssl.so库文件的路径
            System.arraycopy(strPathSsl.getBytes(), 0, ptrByteArraySsl.byValue, 0, strPathSsl.length());
            ptrByteArraySsl.write();
            hCEhomeCMS.NET_ECMS_SetSDKInitCfg(1, ptrByteArraySsl.getPointer());
            //注册服务初始化
            boolean binit = hCEhomeCMS.NET_ECMS_Init();
            //设置HCAapSDKCom组件库文件夹所在路径
            HCISUPCMS.BYTE_ARRAY ptrByteArrayCom = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathCom = System.getProperty("user.dir") + "\\lib\\HCAapSDKCom";        //只支持绝对路径，建议使用英文路径
            System.arraycopy(strPathCom.getBytes(), 0, ptrByteArrayCom.byValue, 0, strPathCom.length());
            ptrByteArrayCom.write();
            hCEhomeCMS.NET_ECMS_SetSDKLocalCfg(5, ptrByteArrayCom.getPointer());

        } else if (osSelect.isLinux()) {
            HCISUPCMS.BYTE_ARRAY ptrByteArrayCrypto = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathCrypto = System.getProperty("user.dir") + "/lib/libcrypto.so"; //Linux版本是libcrypto.so库文件的路径
            System.arraycopy(strPathCrypto.getBytes(), 0, ptrByteArrayCrypto.byValue, 0, strPathCrypto.length());
            ptrByteArrayCrypto.write();
            hCEhomeCMS.NET_ECMS_SetSDKInitCfg(0, ptrByteArrayCrypto.getPointer());

            //设置libssl.so所在路径
            HCISUPCMS.BYTE_ARRAY ptrByteArraySsl = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathSsl = System.getProperty("user.dir") + "/lib/libssl.so";    //Linux版本是libssl.so库文件的路径
            System.arraycopy(strPathSsl.getBytes(), 0, ptrByteArraySsl.byValue, 0, strPathSsl.length());
            ptrByteArraySsl.write();
            hCEhomeCMS.NET_ECMS_SetSDKInitCfg(1, ptrByteArraySsl.getPointer());
            //注册服务初始化
            boolean binit = hCEhomeCMS.NET_ECMS_Init();
            //设置HCAapSDKCom组件库文件夹所在路径
            HCISUPCMS.BYTE_ARRAY ptrByteArrayCom = new HCISUPCMS.BYTE_ARRAY(256);
            String strPathCom = System.getProperty("user.dir") + "/lib/HCAapSDKCom/";        //只支持绝对路径，建议使用英文路径
            System.arraycopy(strPathCom.getBytes(), 0, ptrByteArrayCom.byValue, 0, strPathCom.length());
            ptrByteArrayCom.write();
            hCEhomeCMS.NET_ECMS_SetSDKLocalCfg(5, ptrByteArrayCom.getPointer());

        }
        hCEhomeCMS.NET_ECMS_SetLogToFile(3, "..\\EHomeSDKLog", false);
    }

    /*
     * 中文转unicode编码
     */
    private static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

    public void startCmsListen()
    {
        if (fRegisterCallBack == null) {
            fRegisterCallBack = new FRegisterCallBack();
        }
        System.arraycopy(propertiesUtil.readValue("CmsServerIP").getBytes(), 0, struCMSListenPara.struAddress.szIP, 0, propertiesUtil.readValue("CmsServerIP").length());
        struCMSListenPara.struAddress.wPort = Short.parseShort(propertiesUtil.readValue("CmsServerPort"));
        struCMSListenPara.fnCB = fRegisterCallBack;
        struCMSListenPara.write();
        //启动监听，接收设备注册信息
        CmsHandle = hCEhomeCMS.NET_ECMS_StartListen(struCMSListenPara);
        if (CmsHandle < -1) {
            System.out.println("NET_ECMS_StartListen failed, error code:" + hCEhomeCMS.NET_ECMS_GetLastError());
            hCEhomeCMS.NET_ECMS_Fini();
            return;
        }
        String CmsListenInfo = new String(struCMSListenPara.struAddress.szIP).trim() + "_" + struCMSListenPara.struAddress.wPort;
        System.out.println("注册服务器:" + CmsListenInfo + ",NET_ECMS_StartListen succeed!\n");

    }

    /**
     * ISUP透传接口
     */
    public void CMS_ISAPIPassThrough_AddPerson() throws UnsupportedEncodingException {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
//        String url = "GET /ISAPI/System/deviceInfo";
        String url = "PUT /ISAPI/AccessControl/UserInfo/SetUp?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                "\t\"UserInfo\": {\n" +
                "\t\t\"employeeNo\": \"2745\",\n" +
                "\t\t\"name\": \""+gbEncoding("孔维琪")+"\",\n" +
                "\t\t\"userType\": \"normal\",\n" +
                "\t\t\"Valid\": {\n" +
                "\t\t\t\"enable\": true,\n" +
                "\t\t\t\"beginTime\": \"2022-10-01T00:00:00\",\n" +
                "\t\t\t\"endTime\": \"2030-10-01T00:00:00\",\n" +
                "\t\t\t\"timeType\": \"local\"\n" +
                "\t\t},\n" +
                "\t\t\"belongGroup \": \"1\",\n" +
                "\t\t\"doorRight\": \"1\",\n" +
                "\t\t\"RightPlan\": [{\n" +
                "\t\t\t\"doorNo\": 1,\n" +
                "\t\t\t\"planTemplateNo\": \"1\"\n" +
                "\t\t}]\n" +
                "\t}\n" +
                "}");
        System.out.println(strInBuffer1);
       /* String strInBuffer1 = null;
        try {
            strInBuffer1 = new String(new String("{\n" +
                    "   \"UserInfo\": {\n" +
                    "       \"employeeNo\": \"1360\",\n" +
                    "       \"name\": \"测试\",\n" +
                    "       \"userType\": \"normal\",\n" +
                    "        \"Valid\":{\n" +
                    "            \"enable\":true,\n" +
                    "            \"beginTime\":\"2019-08-01T17:30:08\",\n" +
                    "            \"endTime\":\"2022-08-01T17:30:08\"\n" +
                    "        }\n" +
                    "   }\n" +
                    "}").getBytes("GBK"), Charset.forName("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
        int iStringSize = strInBuffer1.length();
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes("UTF-8"), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
//        m_struParam.pInBuffer = null;
//        m_struParam.dwInSize = 0;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_ISAPIPassThrough(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }
        return;

    }

    /**
     * ISUP透传接口
     */
    public void CMS_ISAPIPassThrough_DelPerson() throws UnsupportedEncodingException {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
//        String url = "GET /ISAPI/System/deviceInfo";
        String url = "PUT /ISAPI/AccessControl/UserInfo/Delete?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                "\t\"UserInfoDelCond\": {\n" +
                "\t\t\"EmployeeNoList\": [{\n" +
                "\t\t\t\"employeeNo\": \"2742\"\n" +
                "\t\t}]\n" +
                "\t}\n" +
                "}");
        System.out.println(strInBuffer1);
       /* String strInBuffer1 = null;
        try {
            strInBuffer1 = new String(new String("{\n" +
                    "   \"UserInfo\": {\n" +
                    "       \"employeeNo\": \"1360\",\n" +
                    "       \"name\": \"测试\",\n" +
                    "       \"userType\": \"normal\",\n" +
                    "        \"Valid\":{\n" +
                    "            \"enable\":true,\n" +
                    "            \"beginTime\":\"2019-08-01T17:30:08\",\n" +
                    "            \"endTime\":\"2022-08-01T17:30:08\"\n" +
                    "        }\n" +
                    "   }\n" +
                    "}").getBytes("GBK"), Charset.forName("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
        int iStringSize = strInBuffer1.length();
        System.out.println(iStringSize);
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes("UTF-8"), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
//        m_struParam.pInBuffer = null;
//        m_struParam.dwInSize = 0;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_ISAPIPassThrough(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }
        return;

    }

    /**
     * ISUP透传接口 -- 查询人员

    public void CMS_ISAPIPassThrough_SaveFace() {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
        String url = "POST /ISAPI/Intelligent/FDLib/FaceDataRecord?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                " \"faceURL\":\"http://192.168.10.59:6011/pic?C554AE8D446E89B7750F0F99207E548Fface.jpeg\",\n" +
                " \"faceLibType\":\"blackFD\",\n" +
                " \"FDID\":\"1\",\n" +
                " \"FPID\":\"1360\",\n" +
                " \"name\":\"" + gbEncoding("测试") +"\",\n" +
                " \"gender\":\"female\"\n" +
                "}\n");
        System.out.println(strInBuffer1);
        int iStringSize = strInBuffer1.length();
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes(), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        if (!hCEhomeCMS.NET_ECMS_PostPTXMLConfig(IsupTest.lLoginID, m_struParam)) {
            int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
            System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
            return;
        } else {
            m_struParam.read();
            ptrOutByte.read();
            System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
        }
        return;

    }
     */
    /**
     * ISUP透传接口 -- 设置人脸库
     */
    public void CMS_ISAPIPassThrough_FDLib() {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
        String url = "POST /ISAPI/Intelligent/FDLib?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                "\"faceLibType\": \"blackFD\",\n" +
                "\"name\": \"FaceLibrary\"\n" +
                "}\n");
        int iStringSize = strInBuffer1.length();
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes(), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_PostPTXMLConfig(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }
        return;

    }

    /**
     * ISUP透传接口
     */
    public void CMS_ISAPIPassThrough_UpdateFace(String faceURl) throws UnsupportedEncodingException {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
//        String url = "GET /ISAPI/System/deviceInfo";
        String url = "PUT /ISAPI/Intelligent/FDLib/FDSetUp?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                " \"faceURL\":\"" + faceURl + "\",\n" +
                " \"faceLibType\":\"blackFD\",\n" +
                " \"FDID\":\"1\",\n" +
                " \"FPID\":\"2741\"\n" +
                "}");
        int iStringSize = strInBuffer1.length();
        System.out.println(strInBuffer1);
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes("UTF-8"), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
//        m_struParam.pInBuffer = null;
//        m_struParam.dwInSize = 0;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_ISAPIPassThrough(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }
        return;

    }


    /**
     * ISUP透传接口 -- 查询人员
     */
    public void CMS_ISAPIPassThrough_Search() {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();
        String url = "POST /ISAPI/AccessControl/UserInfo/Search?format=json";
        HCISUPCMS.BYTE_ARRAY ptrUrl = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl.byValue, 0, url.length());
        ptrUrl.write();
        m_struParam.pRequestUrl = ptrUrl.getPointer();
        m_struParam.dwRequestUrlLen = url.length();
        //透传输入报文
        String strInBuffer1 = new String("{\n" +
                "    \"UserInfoSearchCond\":{\n" +
                "        \"searchID\":\"2677\",\n" +
                "        \"searchResultPosition\":0,\n" +
                "        \"maxResults\":30\n" +
                "    }\n" +
                "}");
        System.out.println(strInBuffer1);
        int iStringSize = strInBuffer1.length();
        HCISUPCMS.BYTE_ARRAY ptrByte = new HCISUPCMS.BYTE_ARRAY(iStringSize);
        System.arraycopy(strInBuffer1.getBytes(), 0, ptrByte.byValue, 0, strInBuffer1.length());
        ptrByte.write();
        m_struParam.pInBuffer=ptrByte.getPointer();
        m_struParam.dwInSize=iStringSize;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_PostPTXMLConfig(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }
        return;

    }


    /**
     * 文件透传接口
     */
    public void CMS_ISAPIPassThroughByFile() {
        HCISUPCMS.NET_EHOME_PTXML_PARAM m_struParam = new HCISUPCMS.NET_EHOME_PTXML_PARAM();
        m_struParam.read();

        String url = "POST /ISAPI/Intelligent/analysisImage/face";
        HCISUPCMS.BYTE_ARRAY ptrUrl1 = new HCISUPCMS.BYTE_ARRAY(url.length());
        System.arraycopy(url.getBytes(), 0, ptrUrl1.byValue, 0, url.length());
        ptrUrl1.write();
        m_struParam.pRequestUrl = ptrUrl1.getPointer();
        m_struParam.dwRequestUrlLen = url.length();

        FileInputStream picfile = null;
        int picdataLength = 0;
        try {

            picfile = new FileInputStream(new File(".\\pic\\FDLib.jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            picdataLength = picfile.available();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (picdataLength < 0) {
            System.out.println("input file dataSize < 0");
            return;
        }

        HCISUPCMS.BYTE_ARRAY ptrpicByte = new HCISUPCMS.BYTE_ARRAY(picdataLength);
        try {
            picfile.read(ptrpicByte.byValue);
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        ptrpicByte.write();
        m_struParam.pInBuffer = ptrpicByte.getPointer();
        m_struParam.dwInSize = picdataLength;
        HCISUPCMS.BYTE_ARRAY ptrOutByte = new HCISUPCMS.BYTE_ARRAY(20 * 1024);
        m_struParam.pOutBuffer = ptrOutByte.getPointer();
        m_struParam.dwOutSize = 20 * 1024;
        m_struParam.write();
        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_ISAPIPassThrough(id, m_struParam)) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_ISAPIPassThrough failed,error：" + iErr);
                return;
            } else {
                m_struParam.read();
                ptrOutByte.read();
                System.out.println("NET_ECMS_ISAPIPassThrough succeed\n" + "ptrOutByte:" + new String(ptrOutByte.byValue).trim());
            }
        }

    }

    /**
     * ISUP NET_ECMS_XMLRemoteControl接口示例
     */
    public void CMS_XMLRemoteControl() {
        m_struOutbuffer = new HCISUPCMS.BYTE_ARRAY(5 * 1024);
        m_struStatusBuffer = new HCISUPCMS.BYTE_ARRAY(2 * 1024);

        struRemoteCtrl = new HCISUPCMS.NET_EHOME_XML_REMOTE_CTRL_PARAM();
        struRemoteCtrl.read();

        struRemoteCtrl.dwSize = struRemoteCtrl.size();
        struRemoteCtrl.dwRecvTimeOut = 5000;
        struRemoteCtrl.dwSendTimeOut = 5000;

        String inputCfg = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>\n" +
                "<PPVSPMessage>\n" +
                "    <Version>4.0</Version>\n" +
                "    <Sequence>1</Sequence>\n" +
                "    <CommandType>REQUEST</CommandType>\n" +
                "    <Method>CONTROL</Method>\n" +
                "    <Command>UPDATE</Command>\n" +
                "    <Params>\n" +
                "        <FTPServerIP>10.17.34.106</FTPServerIP>\n" +
                "        <FTPServerPort>8089</FTPServerPort>\n" +
                "        <Account>hik</Account>\n" +
                "        <Password>hik12345</Password>\n" +
                "        <File>digicap.dav</File>\n" +
                "    </Params>\n" +
                "</PPVSPMessage>";
        m_struInbuffer = new HCISUPCMS.BYTE_ARRAY(inputCfg.length());
        System.arraycopy(inputCfg.getBytes(), 0, m_struInbuffer.byValue, 0, inputCfg.length());
        m_struInbuffer.write();

        struRemoteCtrl.lpInbuffer = m_struInbuffer.getPointer();
        struRemoteCtrl.dwInBufferSize = m_struInbuffer.size();
        struRemoteCtrl.lpOutBuffer = m_struOutbuffer.getPointer();
        struRemoteCtrl.dwOutBufferSize = m_struOutbuffer.size();
        struRemoteCtrl.lpStatusBuffer = m_struStatusBuffer.getPointer();
        struRemoteCtrl.dwStatusBufferSize = m_struStatusBuffer.size();
        struRemoteCtrl.write();

        for (Integer id : IsupBoot.lLoginIDs) {
            if (!hCEhomeCMS.NET_ECMS_XMLRemoteControl(id, struRemoteCtrl, struRemoteCtrl.size())) {
                int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
                System.out.println("NET_ECMS_XMLConfig failed,error：" + iErr);
                return;
            } else {
                struRemoteCtrl.read();
                m_struOutbuffer.read();
                m_struStatusBuffer.read();
                System.out.println("NET_ECMS_XMLConfig succeed：");
                System.out.println("lpOutBuffer:" + new String(m_struOutbuffer.byValue).trim());
                System.out.println("lpStatusBuffer:" + new String(m_struStatusBuffer.byValue).trim());
            }
        }

    }


    /**
     * NET_ECMS_GetDevConfig接口示例
     * @return
     */
    public boolean getDevConfig() {
        boolean bRet = false;
        //  关于报警输入配置条件参数的结构体
        HCISUPCMS.NET_EHOME_ALARMIN_COND netEhomeAlarminCond = new HCISUPCMS.NET_EHOME_ALARMIN_COND();
        netEhomeAlarminCond.read();
        netEhomeAlarminCond.dwSize = netEhomeAlarminCond.size();
        netEhomeAlarminCond.dwAlarmInNum = 1;
        netEhomeAlarminCond.dwPTZChan = 1;
        netEhomeAlarminCond.write();
        HCISUPCMS.NET_EHOME_ALARMIN_CFG netEhomeAlarminCfg = new HCISUPCMS.NET_EHOME_ALARMIN_CFG();
        netEhomeAlarminCfg.read();
        netEhomeAlarminCfg.dwSize = netEhomeAlarminCfg.size();
        netEhomeAlarminCfg.write();

        HCISUPCMS.NET_EHOME_CONFIG strEhomeCfd = new HCISUPCMS.NET_EHOME_CONFIG();
        strEhomeCfd.pCondBuf = netEhomeAlarminCond.getPointer();
        strEhomeCfd.dwCondSize = netEhomeAlarminCond.size();
        strEhomeCfd.pOutBuf = netEhomeAlarminCfg.getPointer();
        strEhomeCfd.dwOutSize = netEhomeAlarminCfg.size();
        strEhomeCfd.pInBuf = null;
        strEhomeCfd.dwInSize = 0;
        strEhomeCfd.write();


       /* bRet = hCEhomeCMS.NET_ECMS_GetDevConfig(IsupTest.lLoginID, 11, strEhomeCfd.getPointer(), strEhomeCfd.size());
        if (!bRet) {
            int dwErr = hCEhomeCMS.NET_ECMS_GetLastError();
            System.out.println("获取报警输入参数失败，Error:" + dwErr);
        }
        //  读取返回的数据
        netEhomeAlarminCfg.read();
        System.out.println("获取报警输入参数成功" + netEhomeAlarminCfg.byAlarmInType);*/
        return bRet;
    }

    /**
     * NET_ECMS_RemoteControl接口示例
     */
    public static void RemoteControl() {
        HCISUPCMS.NET_EHOME_REMOTE_CTRL_PARAM net_ehome_remote_ctrl_param = new HCISUPCMS.NET_EHOME_REMOTE_CTRL_PARAM();
        HCISUPCMS.NET_EHOME_PTZ_PARAM net_ehome_ptz_param = new HCISUPCMS.NET_EHOME_PTZ_PARAM();
        net_ehome_ptz_param.read();
        net_ehome_ptz_param.dwSize = net_ehome_ptz_param.size();
        net_ehome_ptz_param.byPTZCmd = 2;
        net_ehome_ptz_param.byAction = 0;
        net_ehome_ptz_param.bySpeed = 5;
        net_ehome_ptz_param.write();
        net_ehome_remote_ctrl_param.read();
        net_ehome_remote_ctrl_param.dwSize = net_ehome_remote_ctrl_param.size();
        net_ehome_remote_ctrl_param.lpInbuffer = net_ehome_ptz_param.getPointer();
        net_ehome_remote_ctrl_param.dwInBufferSize = net_ehome_ptz_param.size();
        IntByReference channle = new IntByReference(1);
        net_ehome_remote_ctrl_param.lpCondBuffer = channle.getPointer();
        net_ehome_remote_ctrl_param.dwCondBufferSize = 4;
        net_ehome_remote_ctrl_param.write();
        /*boolean b_ptz = hCEhomeCMS.NET_ECMS_RemoteControl(IsupTest.lLoginID, HCISUPCMS.NET_EHOME_PTZ_CTRL, net_ehome_remote_ctrl_param);
        if (!b_ptz) {
            int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
            System.out.println("NET_ECMS_XMLConfig failed,error：" + iErr);
            return;
        }*/
        System.out.println("云台控制调用成功");
    }

    /**
     * ISUP5.0远程升级，需要看设备是否支持此功能
     *
     * @param loginID 设备注册ID
     */
    public void eHome_RomoteUpdate(int loginID) {
        m_struOutbuffer = new HCISUPCMS.BYTE_ARRAY(5 * 1024);
        m_struStatusBuffer = new HCISUPCMS.BYTE_ARRAY(2 * 1024);

        struRemoteCtrl = new HCISUPCMS.NET_EHOME_XML_REMOTE_CTRL_PARAM();
        struRemoteCtrl.read();

        struRemoteCtrl.dwSize = struRemoteCtrl.size();
        struRemoteCtrl.dwRecvTimeOut = 5000 * 60;
        struRemoteCtrl.dwSendTimeOut = 5000;

        String inputCfg = "<?xml version=\"1.0\" encoding=\"GB2312\" ?>\n" +
                "<PPVSPMessage>\n" +
                "    <Version>4.0</Version>\n" +
                "    <Sequence>1</Sequence>\n" +
                "    <CommandType>REQUEST</CommandType>\n" +
                "    <Method>CONTROL</Method>\n" +
                "    <Command>UPDATE</Command>\n" +
                "    <Params>\n" +
                "        <FTPServerIP>10.17.34.106</FTPServerIP>\n" +
                "        <FTPServerPort>8089</FTPServerPort>\n" +
                "        <Account>hik</Account>\n" +
                "        <Password>hik12345</Password>\n" +
                "        <File>digicap.dav</File>\n" +
                "    </Params>\n" +
                "</PPVSPMessage>";
        m_struInbuffer = new HCISUPCMS.BYTE_ARRAY(inputCfg.length());
        System.arraycopy(inputCfg.getBytes(), 0, m_struInbuffer.byValue, 0, inputCfg.length());
        m_struInbuffer.write();
        struRemoteCtrl.lpInbuffer = m_struInbuffer.getPointer();
        struRemoteCtrl.dwInBufferSize = m_struInbuffer.size();
        struRemoteCtrl.lpOutBuffer = m_struOutbuffer.getPointer();
        struRemoteCtrl.dwOutBufferSize = m_struOutbuffer.size();
        struRemoteCtrl.lpStatusBuffer = m_struStatusBuffer.getPointer();
        struRemoteCtrl.dwStatusBufferSize = m_struStatusBuffer.size();
        struRemoteCtrl.write();
        if (!hCEhomeCMS.NET_ECMS_XMLRemoteControl(loginID, struRemoteCtrl, struRemoteCtrl.size())) {
            int iErr = hCEhomeCMS.NET_ECMS_GetLastError();
            System.out.println("NET_ECMS_XMLRemoteControl failed,error：\n" + iErr);
            System.out.println("RomoteUpdate failed\n");
            return;
        } else {
            struRemoteCtrl.read();
            m_struOutbuffer.read();
            m_struStatusBuffer.read();
            System.out.println("NET_ECMS_XMLConfig succeed\n");
            System.out.println("lpOutBuffer:" + new String(m_struOutbuffer.byValue).trim());
            System.out.println("lpStatusBuffer:" + new String(m_struStatusBuffer.byValue).trim());
            System.out.println("RomoteUpdate succeed\n");
        }
    }

    /**
     * 根据不同操作系统选择不同的库文件和库路径
     *
     * @return
     */
    private static boolean CreateSDKInstance() {
        if (hCEhomeCMS == null) {
            synchronized (HCISUPCMS.class) {
                String strDllPath = "";
                try {
                    //System.setProperty("jna.debug_load", "true");
                    if (osSelect.isWindows())
                        //win系统加载库路径
                        strDllPath = System.getProperty("user.dir") + "\\lib\\HCISUPCMS.dll";
                    else if (osSelect.isLinux())
                        //Linux系统加载库路径
                        strDllPath = System.getProperty("user.dir")+"/lib/libHCISUPCMS.so";
                    hCEhomeCMS = (HCISUPCMS) Native.loadLibrary(strDllPath, HCISUPCMS.class);
                } catch (Exception ex) {
                    System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage());
                    return false;
                }
            }
        }
        return true;
    }

    //注册回调函数
    public class FRegisterCallBack implements HCISUPCMS.DEVICE_REGISTER_CB {
        public boolean invoke(int lUserID, int dwDataType, Pointer pOutBuffer, int dwOutLen, Pointer pInBuffer, int dwInLen, Pointer pUser) {
            System.out.println("FRegisterCallBack, dwDataType:" + dwDataType + ", lUserID:" + lUserID);
            switch (dwDataType) {
                case 0:  //ENUM_DEV_ON
                    HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12 strDevRegInfo = new HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12();
                    strDevRegInfo.write();
                    Pointer pDevRegInfo = strDevRegInfo.getPointer();
                    pDevRegInfo.write(0, pOutBuffer.getByteArray(0, strDevRegInfo.size()), 0, strDevRegInfo.size());
                    strDevRegInfo.read();
                    HCISUPCMS.NET_EHOME_SERVER_INFO_V50 strEhomeServerInfo = new HCISUPCMS.NET_EHOME_SERVER_INFO_V50();
                    strEhomeServerInfo.read();
                    //strEhomeServerInfo.dwSize = strEhomeServerInfo.size();
                    byte[] byCmsIP = new byte[0];
                    //设置报警服务器地址、端口、类型
                    byCmsIP = propertiesUtil.readValue("AlarmServerIP").getBytes();
                    System.arraycopy(byCmsIP, 0, strEhomeServerInfo.struUDPAlarmSever.szIP, 0, byCmsIP.length);
                    System.arraycopy(byCmsIP, 0, strEhomeServerInfo.struTCPAlarmSever.szIP, 0, byCmsIP.length);
                    //报警服务器类型：0- 只支持UDP协议上报，1- 支持UDP、TCP两种协议上报 2-MQTT

                    strEhomeServerInfo.dwAlarmServerType = Integer.parseInt(propertiesUtil.readValue("AlarmServerType"));
                    strEhomeServerInfo.struTCPAlarmSever.wPort = Short.parseShort(propertiesUtil.readValue("AlarmServerTCPPort"));
                    strEhomeServerInfo.struUDPAlarmSever.wPort = Short.parseShort(propertiesUtil.readValue("AlarmServerUDPPort"));

                    byte[] byClouldAccessKey = "test".getBytes();
                    System.arraycopy(byClouldAccessKey, 0, strEhomeServerInfo.byClouldAccessKey, 0, byClouldAccessKey.length);
                    byte[] byClouldSecretKey = "12345".getBytes();
                    System.arraycopy(byClouldSecretKey, 0, strEhomeServerInfo.byClouldSecretKey, 0, byClouldSecretKey.length);
                    strEhomeServerInfo.dwClouldPoolId = 1;

                    //设置图片存储服务器地址、端口、类型
                    byte[] bySSIP = new byte[0];
                    bySSIP = propertiesUtil.readValue("PicServerIP").getBytes();
                    System.arraycopy(bySSIP, 0, strEhomeServerInfo.struPictureSever.szIP, 0, bySSIP.length);
                    strEhomeServerInfo.struPictureSever.wPort = Short.parseShort(propertiesUtil.readValue("PicServerPort"));
                    strEhomeServerInfo.dwPicServerType = Integer.parseInt(propertiesUtil.readValue("PicServerType"));    //存储服务器（SS）类型：0-Tomcat，1-VRB，2-云存储，3-KMS，4-ISUP5.0。
                    strEhomeServerInfo.write();
                    dwInLen = strEhomeServerInfo.size();
                    pInBuffer.write(0, strEhomeServerInfo.getPointer().getByteArray(0, dwInLen), 0, dwInLen);

                    System.out.println("Device online, DeviceID is:" + new String(strDevRegInfo.struRegInfo.byDeviceID).trim());
                    IsupBoot.lLoginIDs.add(lUserID);
                    return true;
                case 3: //ENUM_DEV_AUTH
                    strDevRegInfo = new HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12();
                    strDevRegInfo.write();
                    pDevRegInfo = strDevRegInfo.getPointer();
                    pDevRegInfo.write(0, pOutBuffer.getByteArray(0, strDevRegInfo.size()), 0, strDevRegInfo.size());
                    strDevRegInfo.read();
                    byte[] bs = new byte[0];
                    String szEHomeKey = propertiesUtil.readValue("ISUPKey"); //ISUP5.0登录校验值
                    bs = szEHomeKey.getBytes();
                    pInBuffer.write(0, bs, 0, szEHomeKey.length());
                    break;
                case 4: //HCISUPCMS.ENUM_DEV_SESSIONKEY
                    strDevRegInfo = new HCISUPCMS.NET_EHOME_DEV_REG_INFO_V12();
                    strDevRegInfo.write();
                    pDevRegInfo = strDevRegInfo.getPointer();
                    pDevRegInfo.write(0, pOutBuffer.getByteArray(0, strDevRegInfo.size()), 0, strDevRegInfo.size());
                    strDevRegInfo.read();
                    HCISUPCMS.NET_EHOME_DEV_SESSIONKEY struSessionKey = new HCISUPCMS.NET_EHOME_DEV_SESSIONKEY();
                    System.arraycopy(strDevRegInfo.struRegInfo.byDeviceID, 0, struSessionKey.sDeviceID, 0, strDevRegInfo.struRegInfo.byDeviceID.length);
                    System.arraycopy(strDevRegInfo.struRegInfo.bySessionKey, 0, struSessionKey.sSessionKey, 0, strDevRegInfo.struRegInfo.bySessionKey.length);
                    struSessionKey.write();
                    Pointer pSessionKey = struSessionKey.getPointer();
                    hCEhomeCMS.NET_ECMS_SetDeviceSessionKey(pSessionKey);
                    AlarmDemo.hcEHomeAlarm.NET_EALARM_SetDeviceSessionKey(pSessionKey);
                    break;
                case 5: //HCISUPCMS.ENUM_DEV_DAS_REQ
                    String dasInfo = "{\n" +
                            "    \"Type\":\"DAS\",\n" +
                            "    \"DasInfo\":{\n" +
                            "        \"Address\":\"" + propertiesUtil.readValue("DasServerIP") + "\",\n" +
                            "        \"Domain\":\"\",\n" +
                            "        \"ServerID\":\"\",\n" +
                            "        \"Port\":" + propertiesUtil.readValue("DasServerPort") + ",\n" +
                            "        \"UdpPort\":\n" +
                            "    }\n" +
                            "}";
                    System.out.println(dasInfo);
                    byte[] bs1 = dasInfo.getBytes();
                    pInBuffer.write(0, bs1, 0, dasInfo.length());
                    break;
                default:
                    System.out.println("FRegisterCallBack default type:" + dwDataType);
                    break;
            }
            return true;
        }
    }


}
